var socket
function connect(opener, resiver, url) {
    if ('WebSocket' in window) {
        socket = new WebSocket("ws://" + window.location.host + url);
        socket.onopen = function () {
            opener.call();
        }

        socket.onclose = function () {
            console.log('Disconnecting connection');
            socket.close();
        }

        socket.onmessage = function (evt) {
            var received_msg = evt.data;
            resiver(received_msg);
        }

    } else {
        console.log('Websocket not supported');
    }
}

function send(message) {
    if (socket)
        socket.send(message);
}

function closeWebsocket() {
    if (socket)
        socket.close();
}